package com.sikaryofficial.backend.utils;

import com.sikaryofficial.backend.domain.vo.store.StoreMetricVO;

import java.util.List;
import java.util.function.Function;

/**
 * @author : qinjinyuan
 * @desc : Min-Max 标准化工具类
 * @date : 2025/08/22 17:07
 */
public class MinMaxNormalizer {

    public static void normalize(List<StoreMetricVO> metrics, Function<StoreMetricVO, Double> getter, ToDoubleBiConsumer<StoreMetricVO> setter) {
        double min = metrics.stream().mapToDouble(getter::apply).min().orElse(0);
        double max = metrics.stream().mapToDouble(getter::apply).max().orElse(1);

        double finalMin = min;
        double finalMax = max;
        metrics.forEach(m -> {
            double raw = getter.apply(m);
            double norm = (finalMax - finalMin) == 0 ? 0 : (raw - finalMin) / (finalMax - finalMin);
            setter.accept(m, norm);
        });
    }

    @FunctionalInterface
    public interface ToDoubleBiConsumer<T> {
        void accept(T t, double value);
    }
}


